package com.julian.warehouse;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.julian.warehouse.activity.BackupActivity;
import com.julian.warehouse.activity.ChukuActivity;
import com.julian.warehouse.activity.KucunActivity;
import com.julian.warehouse.activity.PandianActivity;
import com.julian.warehouse.activity.RecordsActivity;
import com.julian.warehouse.activity.RukuActivity;
import com.julian.warehouse.base.SPBaseActivity;
import com.julian.warehouse.dialog.MyAlertDialog;
import com.julian.warehouse.interfaces.OnBooleanListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends SPBaseActivity implements AdapterView.OnItemClickListener{

    private GridView gridView;
    private List<Map<String,Object>> dataList;
    private int[] icon={R.mipmap.kucun,R.mipmap.ruku,R.mipmap.chuku,R.mipmap.pandian,R.mipmap.record,R.mipmap.backup};
    private String[] iconName={"商品库存","入库","出库","库存盘点","出入库记录","数据库备份"};
    private SimpleAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.init();
        setHeadTitle(R.string.app_name);

        requestPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, new OnBooleanListener() {
            @Override
            public void onClick(boolean isAllHave, int[] position) {
                if (!isAllHave){
                   showMyDialog(false, "权限不足，请手动去设置中开启应用权限，系统将关闭", "退出", new MyAlertDialog.OnMyClickListener() {
                       @Override
                       public void onClick(MyAlertDialog dialog) {
                           finish();
                           System.exit(0);
                       }
                   });
                }
            }
        });

    }

    @Override
    public void initView() {
        gridView = (GridView) findViewById(R.id.gridView);

    }

    @Override
    public void initData() {
        dataList=new ArrayList<>();
        adapter=new SimpleAdapter(this,getData(),R.layout.item_gridview,new String[]{"image","text"},
                new int[]{R.id.image,R.id.text});
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //showToast("点击了"+iconName[position]);
        switch (position){
            case 0:
                startActivity(new Intent(this, KucunActivity.class));
                break;
            case 1:
                startActivity(new Intent(this, RukuActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, ChukuActivity.class));
                break;
            case 3:
                startActivity(new Intent(this, PandianActivity.class));
                break;
            case 4:
                startActivity(new Intent(this, RecordsActivity.class));
                break;
            case 5:
                startActivity(new Intent(this, BackupActivity.class));
                break;
            default:break;
        }

    }


    private List<Map<String,Object>> getData(){
        for(int i=0;i<icon.length;i++){
            Map<String,Object>map=new HashMap<>();
            map.put("image",icon[i]);
            map.put("text",iconName[i]);
            dataList.add(map);
        }
        return dataList;
    }


}
